<?php
/**
 * AreaModel.class.php      国家地区模型
 *
 * @author                  mashanling(msl-138@163.com)
 * @date                    22:03 2012-7-18
 * @last modify             2012-09-10 16:56:23 by mashanling
 */

!defined('ROOT') && exit('Access Denied');

class AreaModel extends CommonModel {
    protected $auto = array(//自动填充
        'is_show'    => 'getCheckboxValue',//是否显示
        'sort_order' => 'unsigned#data',//排序 by mashanling on 2012-09-10 16:36:38
    );

    /**
     * 数据表字段信息
     * filter: 数据类型，array(数据类型(string,int,float...),Filter::方法参数1,参数2...)
     * validate: 自动验证，支持多个验证规则
     *
     * @see Model.class.php create()方法对数据过滤
     * @see CommonModel.class.php __construct()方法设置自动验证字段_validate
     */
    protected $db_fields = array (
        'area_id'          => array('filter' => 'int', 'validate' => 'unsigned#PRIMARY_KEY,INVALID'),//自增主键
        'parent_id'        => array('filter' => 'int', 'validate' => 'unsigned#PLEASE_SELECT,PARENT_AREA'),//父级地区id

        //地区名称
        'area_name'        => array('validate' => array('checkAreaName#PLEASE_ENTER,MODULE_NAME_AREA,NAME#data', 'checkLength#MODULE_NAME_AREA,NAME#value|0|50')),
        'area_code'        => array('validate' => 'checkLength#AREA_CODE#value|0|15'),

        'is_show'          => array('filter' => 'int'),//是否显示
        'sort_order'       => array('filter' => 'int', 'validate' => 'unsigned#ORDER#-2'),//排序
    );
    protected $pk = 'area_id';//主键
    protected $true_table_name = TB_AREA;//表

	/**
     * 删除后置操作
     *
     * @author                  mashanling(msl-138@163.com)
     * @date                    2012-07-19 13:36:28
     * @last modify             2012-09-10 17:48:55 by mashanling
     *
     * @see Model::_after_delete()
     *
     * @return void 无返回值
     */
    protected function _after_delete($data, $options = array()) {
        $this->module->create();
    }

    /**
     * 插入后置操作
     *
     * @author                  mashanling(msl-138@163.com)
     * @date                    2012-07-19 13:35:22
     * @last modify             2012-09-10 16:55:15 by mashanling
     *
     * @see Model::_after_insert()
     *
     * @return void 无返回值
     */
    protected function _after_insert($data, $options) {
        $pk_field = $this->getPk();
        !isset($data['sort_order']) && $this->db->execute('UPDATE ' . TB_AREA . " SET sort_order={$data[$pk_field]} WHERE {$pk_field}={$data[$pk_field]}");
    }

    /**
     * 更新后置操作
     *
     * @author                  mashanling(msl-138@163.com)
     * @date                    2012-07-19 13:35:56
     * @last modify             2012-07-19 13:35:56 by mashanling
     *
     * @see Model::_after_update()
     *
     * @return void 无返回值
     */
    protected function _after_update($data, $options) {
        $count = count($data);

        if ($count == 1) {

            if (isset($data['is_show'])) {//显示，隐藏
                $this->module->create();
            }
        }
    }

    /**
     * 验证地区名称是否已经存在
     *
     * @param string $name 地区名称
     * @param array  $data   _POST数据
     *
     * @return mixed 验证成功，返回true。否则，如果未输入，返回提示信息，否则返回false
     */
    protected function checkAreaName($name, $data) {

        if ($name === '') {//如果未输入，提示输入
            return false;
        }

        $parent_id = isset($data['parent_id']) ? $data['parent_id'] : 0;
        $area_id   = isset($data[$this->pk]) ? $data[$this->pk] : 0;

        //缓存数据比较大，通过循环缓存判断改为读数据库判断 by mashanling on 2012-08-15 11:24:58
        $info = $this->where("parent_id={$parent_id} AND area_name='{$name}' AND {$this->pk}!={$area_id}")->find();
        return !$info ? true : L('MODULE_NAME_AREA,EXIST') . ': <br /><span style="color: #666;font-weight: bold;">' . $this->module->nav($info[$this->pk], 'area_name') . '</span>';
    }
}